package com.example.gameapplication.view;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.example.gameapplication.R;
import com.example.gameapplication.activity.ControlDialogActivity;
import com.example.gameapplication.activity.MainActivity;
import com.example.gameapplication.bullet.BossSkill;
import com.example.gameapplication.bullet.BulletBuff;
import com.example.gameapplication.bullet.Skill;
import com.example.gameapplication.bullet.WindmillSkill;
import com.example.gameapplication.database.DataUtils;
import com.example.gameapplication.plant.BigPlane;
import com.example.gameapplication.plant.BossPlane;
import com.example.gameapplication.plant.MainPlane;
import com.example.gameapplication.plant.MiddlePlane;
import com.example.gameapplication.plant.SmallPlane;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;

//尝试将GameView分离成3个层次，该画面为敌人战机层次
public class MiddlePlaneGameView extends SurfaceView implements SurfaceHolder.Callback, Runnable , DataUtils.PlayButtonCallBack {
    private SurfaceHolder mHolder;
    private ExecutorService executorService;
    private Canvas mCanvas;
    private Paint paint;
    List<MiddlePlane> middlePlanes;
    private boolean startScreen;//后台回来画面是黑色的，需要播放一帧。
    private MainPlane mainPlane;


    public MiddlePlaneGameView(Context context) {
        super(context);
        init();
    }

    public MiddlePlaneGameView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MiddlePlaneGameView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    protected void init() {
        mHolder = getHolder();
        mHolder.addCallback(this);
        setFocusable(true);
        setFocusableInTouchMode(true);
        this.setKeepScreenOn(true);
//        setZOrderOnTop(true);// 在最顶层，会遮挡一切view
//        setZOrderMediaOverlay(true);// 如已绘制SurfaceView则在surfaceView上一层绘制。
        mHolder.setFormat(PixelFormat.TRANSLUCENT);
        executorService = DataUtils.getExecutorService();//用线程池，不要频繁创建线程，否则app会崩。这里我使用单线程的线程池。
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        Resources resources = getResources();
        DataUtils.setGameViewWidth(getMeasuredWidth());
        DataUtils.setGameViewHight(getMeasuredHeight());
        paint = DataUtils.getPaint();
        mainPlane = DataUtils.getmainPlane(resources);
        middlePlanes=DataUtils.getMiddlePlanes();
        startScreen = true;
        DataUtils.setMiddlePlayButtonCallBack(this);
        executorService.execute(this);
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        DataUtils.setIsPlaying(false);
        if (middlePlanes != null) {
            for (MiddlePlane middlePlane : middlePlanes) {
                middlePlane.dataDestroyed();
            }
        }

    }

    @Override
    public void run() {
        while (DataUtils.isIsPlaying() || startScreen) {
            startScreen = false;//播放完1帧之后改为false
            try {

                long startTime = System.currentTimeMillis();
                //锁定画布并返回画布对象
                mCanvas = mHolder.lockCanvas();
//                mCanvas = mHolder.lockHardwareCanvas();//硬件加速的画板，但是当cpu高使用率，比如说战机同时出现好几百台，反而会卡，表现不平稳
                //接下去就是在画布上进行一下draw
                mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

                for (int i = 0; i < middlePlanes.size(); i++) {
                    middlePlanes.get(i).drawSelf(mCanvas);
                }
                addmiddleplane();
                long endTime = System.currentTimeMillis();
                if (startTime - endTime < 80) {
                    Thread.sleep(60);
                }
            } catch (Exception e) {
                Log.e("AACC", "Exception:MiddlePlaneGameView " + e.toString());
                e.printStackTrace();
            } finally {
                //当画布内容不为空时，才post，避免出现黑屏的情况。
                if (mCanvas != null)
                    mHolder.unlockCanvasAndPost(mCanvas);
            }
        }
    }





    private void addmiddleplane() {
        if (middlePlanes.size() == DataUtils.getMiddleMaxSum()) return;
        while (middlePlanes.size() > DataUtils.getMiddleMaxSum()) {
            middlePlanes.get(0).dataDestroyed();
            middlePlanes.remove(0);
        }
        if (middlePlanes.size() == DataUtils.getMiddleMaxSum()) return;
        MiddlePlane smallPlane = new MiddlePlane(getResources(), paint);
        smallPlane.setMainPlane(mainPlane);
        middlePlanes.add(smallPlane);
    }


    @Override
    public void playButtonOncleck() {
        startScreen = true;
        executorService.execute(this);
    }
}